package com.flymobi.mshop.util.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.flymobi.mshop.util.JsonHttpClient;

/**
 * @author gaoxf
 * 
 */
public class ProductService extends IService {
	public static List<Map<String, Object>> cache;

	public static List<Map<String, Object>> list(String clsId,String rank) {
		return list("", clsId,rank);
	}

	public static List<Map<String, Object>> list(String key, String clsId,String rank) {
		cache = new ArrayList<Map<String, Object>>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", Action.LIST_PROD);
		params.put("id", clsId);
		params.put("key", key);
		params.put("rank", rank);
		JSONArray ja = JsonHttpClient.post(serviceUrl, params);
		if(ja==null){
			return null;
		}
		Log.d("json", ja.toString());
		int size = ja.length();
		for (int i = 0; i < size; i++) {
			JSONObject jo;
			try {
				jo = ja.getJSONObject(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", jo.getString("id"));
				map.put("name", jo.getString("name"));
				map.put("saleprice", jo.getString("saleprice"));
				map.put("price", jo.getString("price"));
				map.put("imgpath", jo.getString("imgpath"));
				cache.add(map);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return cache;
	}
	
	public static int getCount(String key, String clsId) {
		String count="0";
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", Action.GET_PROD_COUNT);
		params.put("id", clsId);
		params.put("key", key);
		JSONArray ja = JsonHttpClient.post(serviceUrl, params);
		Log.d("json", ja.toString());
		int size = ja.length();
		for (int i = 0; i < size; i++) {
			JSONObject jo;
			try {
				jo = ja.getJSONObject(i);
				count=jo.getString("count");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return Integer.parseInt(count);
	}
	
	public static Map<String, Object> get(String id){
		Map<String, Object> rst = new HashMap<String, Object>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", Action.GET_PROD);
		params.put("id", id);
		JSONArray ja = JsonHttpClient.post(serviceUrl, params);
		Log.d("json", ja.toString());
		int size = ja.length();
		for (int i = 0; i < size; i++) {
			JSONObject jo;
			try {
				jo = ja.getJSONObject(i);
				rst.put("id", jo.getString("id"));
				rst.put("code", jo.getString("code"));
				rst.put("name", jo.getString("name"));
				rst.put("saleprice", jo.getString("saleprice"));
				rst.put("price", jo.getString("price"));
				rst.put("img",jo.getString("img"));
				rst.put("intro", jo.getString("intro"));
				JSONArray imgArray=jo.getJSONArray("imgList");
				String[] imgList=new String[imgArray.length()];
				for(int j=0;j<imgArray.length();j++){
					imgList[j]=imgArray.getJSONObject(j).getString("path");
				}
				rst.put("imgList", imgList);
				JSONArray propValueArray=jo.getJSONArray("propValueList");
				Map<String,String[]> propValueList=new HashMap<String,String[]>();
				for(int j=0;j<propValueArray.length();j++){
					JSONArray valueArray=propValueArray.getJSONObject(j).getJSONArray("valueList");
					String[] valueList=new String[valueArray.length()];
					for(int k=0;k<valueArray.length();k++){
						valueList[k]=valueArray.getString(k);
					}
					propValueList.put(propValueArray.getJSONObject(j).getString("name"),valueList);
				}
				rst.put("propValueList", propValueList);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return rst;
	}
}
